package com.pp.controller;


import com.pp.bean.PpUser;
import com.pp.bean.User;
import com.pp.commne.Constant;
import com.pp.service.IPpImageService;
import com.pp.service.IUserService;
import com.pp.util.weixin.WxpubOAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    IPpImageService ppImageService;
    /**
     * 登陆页面
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Object login( )  {
       return "login";
    }
    /**
     * 登陆
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseBody
    public boolean login(String userName,String passWord , HttpSession session)  {
       return userService.login(userName,passWord,session);
    }
    /**
     * 后台管理
     *
     * @return
     */
    @RequestMapping(value = "/main.html", method = RequestMethod.GET)
    public String main(  )  {
       return "admin";
    }
    /**
     * 后台管理--ppuser管理
     *
     * @return
     */
    @RequestMapping(value = "/ppuser.html", method = RequestMethod.GET)
    public String ppuser(  )  {
       return "ppuser";
    }    /**
     * 后台管理--发货管理
     *
     * @return
     */
    @RequestMapping(value = "/push_stock.html", method = RequestMethod.GET)
    public String push_stock(  )  {
       return "push_stock";
    }
    /**
     * 后台管理--欢迎
     *
     * @return
     */
    @RequestMapping(value = "/welcome.html", method = RequestMethod.GET)
    public String welcome(  )  {
       return "welcome";
    }


    @RequestMapping(value = "index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }


    /**
     * 后台管理---更换轮播图 页面
     *
     * @return
     */
    @RequestMapping(value = "/updateImage.html", method = RequestMethod.GET)
    public String updateImage( Model model )  {
        model.addAttribute("images",ppImageService.listByMap(new HashMap<>()));
        return "updateImage";
    }

    /**
     * 后台管理---修改用户账户页面
     *
     * @return
     */
    @RequestMapping(value = "/updateAdmin.html", method = RequestMethod.GET)
    public String updateAdminHtml(  )  {
        return "update_admin";
    }
    /**
     * 后台管理---修改用户账户
     *
     * @return
     */
    @RequestMapping(value = "/updateAdmin")
    @ResponseBody
    public boolean updateAdmin(String account,String pass ,HttpSession session )  {
        User user=new User();
        user.setId(((User)session.getAttribute("user")).getId());
        user.setAccount(account);
        user.setPass(pass);
        if( userService.updateById(user)){
            session.setAttribute("user",null);
            return true;
        }else{
            return  false;
        }
    }

    @RequestMapping(value = "/chanpin",method = RequestMethod.GET)
    public String chanpin(){
        return "chanpin";
    }

    @RequestMapping(value = "/huodong",method = RequestMethod.GET)
    public String huodong(){
        return "huodong";
    }


}
